﻿using AutoScaleHelper;
using HZH_Controls.Controls;
using LituoPro.database;
using LituoPro.database.entity.bs;
using LituoPro.database.entity.req;
using LituoPro.database.entity.res;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LituoPro
{
    public partial class DataDebugForm : Form
    {
        private AutoScale autoScale = null;
        //总页数
        private int countItem = 0;
        //当前页
        private int positionItem = 1;

        private int pageSize = 16;

        private int pageNo = 1;


        private SqlLiteDataHelper sqlLiteDataHelper = new SqlLiteDataHelper();

        public DataDebugForm()
        {
            InitializeComponent();
            this.getAutoScaleInstance();
            autoScale.SetContainer(this);
            this.initDataGridViewColumn();

        }

        private AutoScale getAutoScaleInstance()
        {
            if (autoScale == null)
            {
                autoScale = new AutoScale();
            }

            return autoScale;
        }


        private void initDataGridViewColumn()
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "eventId", HeadText = "事件ID", Width = 110, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "devType", HeadText = "设备类型", Width = 100, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "aiContent", HeadText = "AI", Width = 270, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "diContent", HeadText = "DI", Width = 270, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "aoContent", HeadText = "AO", Width = 240, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "doContent", HeadText = "DO", Width = 190, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "createTime", HeadText = "接收时间", Width = 195, WidthType = SizeType.Absolute });
            this.ucHisDataGridView.Columns = lstCulumns;
        }



        private void setPageDataSource(int pageNumber)
        {
            string startDateTime = this.startDateTimePicker.Value.ToString();
            string endDateTime = this.endDateTimePicker.Value.ToString();
            string devType = String.Empty;
            if (this.devComboBox.SelectedItem != null)
            {
                devType = this.devComboBox.SelectedItem.ToString();
            }
            int offset = (pageNumber - 1) * pageSize;
            BasePage basePage = new BasePage(pageNumber, pageSize, offset);
            QueryDataReq queryDataReq = new QueryDataReq(startDateTime, endDateTime, devType);
            List<QueryDataRes> queryDatas = sqlLiteDataHelper.QueryDataPage(queryDataReq, basePage);
            long total = sqlLiteDataHelper.QueryDataCount(queryDataReq);
            int totalPages = (int)Math.Ceiling((double)total / pageSize);
            countItem = totalPages;
            List<object> lstSource = new List<object>();
            for (int i = 0; i < queryDatas.Count; i++)
            {
                QueryDataRes queryDataEntity = queryDatas[i];
                lstSource.Add(queryDataEntity);
            }
            this.ucHisDataGridView.DataSource = lstSource;
        }




        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            positionItem = 1;
            this.setPageDataSource(positionItem);
            this.setBindingNavigator();
        }

        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            positionItem = positionItem - 1;
            this.setPageDataSource(positionItem);
            this.setBindingNavigator();
        }

        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            positionItem = positionItem + 1;
            this.setPageDataSource(positionItem);
            this.setBindingNavigator();
        }

        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            this.setPageDataSource(countItem);
            positionItem = countItem;
            this.setBindingNavigator();
        }


        private void setBindingNavigator()
        {
            this.bindingNavigatorCountItem.Text = countItem.ToString();
            this.bindingNavigatorPositionItem.Text = positionItem.ToString();
            //当前页为第一页并且总页数大于一页
            if (positionItem == 1 && countItem > 1) {
                bindingNavigatorMoveFirstItem.Enabled = false;
                bindingNavigatorMovePreviousItem.Enabled = false;
                bindingNavigatorMoveLastItem.Enabled = true;
                bindingNavigatorMoveNextItem.Enabled = true;
            }
            //当前页为最后一页并且总页数大于一页
            else if (positionItem == countItem && countItem > 1)
            {
                bindingNavigatorMoveFirstItem.Enabled = true;
                bindingNavigatorMovePreviousItem.Enabled = true;
                bindingNavigatorMoveLastItem.Enabled = false;
                bindingNavigatorMoveNextItem.Enabled = false;
            }
            //当前页为第二页或者之后并且总页数大于一页
            else if (positionItem >= 2 && countItem > 1)
            {
                bindingNavigatorMoveFirstItem.Enabled = true;
                bindingNavigatorMovePreviousItem.Enabled = true;
                bindingNavigatorMoveLastItem.Enabled = true;
                bindingNavigatorMoveNextItem.Enabled = true;
            }
            //总页数只有一页
           else  if ( countItem == 1)
            {
                bindingNavigatorMoveFirstItem.Enabled = false;
                bindingNavigatorMovePreviousItem.Enabled = false;
                bindingNavigatorMoveLastItem.Enabled = false;
                bindingNavigatorMoveNextItem.Enabled = false;
            }


        }

        private void queryButton_Click(object sender, EventArgs e)
        {
            this.setPageDataSource(pageNo);
            this.setBindingNavigator();
        }

        private void DataDebugForm_SizeChanged(object sender, EventArgs e)
        {
            this.SuspendLayout();
            this.getAutoScaleInstance();
            autoScale.UpdateControlsLayout();
            this.ResumeLayout();
        }
    }
}
